package com.chenbei.erp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.common.exception.LogicHandleException;
import com.chenbei.erp.domain.Supplier;
import com.chenbei.erp.mapper.SupplierMapper;
import com.chenbei.erp.service.ISupplierContactService;
import com.chenbei.erp.service.ISupplierInvoiceService;
import com.chenbei.erp.service.ISupplierService;
import com.chenbei.erp.vo.req.SupplierReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

@Service
public class SupplierService extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {

    @Autowired
    private ISupplierInvoiceService invoiceService;

    private final Object lock = new Object();

    @Transactional
    @Override
    public void add(Supplier supplier) {
        synchronized (lock) {
            LambdaQueryWrapper<Supplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Supplier::getName, supplier.getName());
            if (count(lambdaQueryWrapper) > 0) {
                throw new LogicHandleException("供应商已存在");
            }
            supplier.setCode(buildCode());

            if (save(supplier)) {
                if (supplier.getInvoice() != null) {
                    supplier.getInvoice().setSupplierId(supplier.getId());
                    invoiceService.addOrUpdate(supplier.getInvoice());
                }
            } else {
                throw new LogicHandleException("添加失败");
            }
        }
    }

    @Transactional
    @Override
    public void update(Supplier supplier) {
        LambdaQueryWrapper<Supplier> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(Supplier::getId, supplier.getId())
                .eq(Supplier::getName, supplier.getName());
        if (count(lambdaQueryWrapper) > 0) {
            throw new LogicHandleException("供应商已存在");
        }
        if(updateById(supplier)) {
            if (supplier.getInvoice() != null) {
                supplier.getInvoice().setSupplierId(supplier.getId());
                invoiceService.addOrUpdate(supplier.getInvoice());
            }
        }
    }

    @Override
    public Page<Supplier> pageList(SupplierReq supplierReq) {
        Page<Supplier> pageConfig = new Page<>(supplierReq.getPageNum(), supplierReq.getPageSize());
        return baseMapper.pageList(pageConfig, supplierReq);
    }

    @Override
    public Supplier detail(Long id) {
        Supplier supplier = baseMapper.getById(id);
        if (supplier != null) {
            supplier.setInvoice(invoiceService.getBySupplierId(id));
        }
        return supplier;
    }

    private String buildCode() {
        String prefix = "SUPP";
        String categoryCode = "00";
//        String extraCode = "0000";        //额外码（预留字段）
        long count = count() + 1;
        String serialNumber = String.format("%04d", count);
        return String.join("-", Arrays.asList(prefix, categoryCode, serialNumber));
    }
}
